import React, { useState, useEffect } from 'react';
import { Card, Table, Button, Space, Modal, Form, Input, Select, message } from 'antd';
import { PlusOutlined, EditOutlined, DeleteOutlined } from '@ant-design/icons';
import { PageContainer } from '@ant-design/pro-layout';
import { getUniversityList, addUniversity, updateUniversity, deleteUniversity } from '@/services/university';

const { Option } = Select;

const University: React.FC = () => {
  const [form] = Form.useForm();
  const [loading, setLoading] = useState(false);
  const [data, setData] = useState([]);
  const [modalVisible, setModalVisible] = useState(false);
  const [modalTitle, setModalTitle] = useState('');
  const [currentRecord, setCurrentRecord] = useState<any>(null);

  const columns = [
    {
      title: '大学名称',
      dataIndex: 'universityName',
      key: 'universityName',
    },
    {
      title: '大学代码',
      dataIndex: 'universityCode',
      key: 'universityCode',
    },
    {
      title: '大学类型',
      dataIndex: 'universityType',
      key: 'universityType',
    },
    {
      title: '所属省份',
      dataIndex: 'province',
      key: 'province',
    },
    {
      title: '所属城市',
      dataIndex: 'city',
      key: 'city',
    },
    {
      title: '状态',
      dataIndex: 'status',
      key: 'status',
      render: (status: string) => (
        <span style={{ color: status === '0' ? '#52c41a' : '#ff4d4f' }}>
          {status === '0' ? '正常' : '停用'}
        </span>
      ),
    },
    {
      title: '操作',
      key: 'action',
      render: (_: any, record: any) => (
        <Space size="middle">
          <Button
            type="link"
            icon={<EditOutlined />}
            onClick={() => handleEdit(record)}
          >
            编辑
          </Button>
          <Button
            type="link"
            danger
            icon={<DeleteOutlined />}
            onClick={() => handleDelete(record)}
          >
            删除
          </Button>
        </Space>
      ),
    },
  ];

  const fetchData = async () => {
    setLoading(true);
    try {
      const response = await getUniversityList();
      setData(response.data);
    } catch (error) {
      message.error('获取数据失败');
    }
    setLoading(false);
  };

  useEffect(() => {
    fetchData();
  }, []);

  const handleAdd = () => {
    setModalTitle('新增大学');
    setCurrentRecord(null);
    form.resetFields();
    setModalVisible(true);
  };

  const handleEdit = (record: any) => {
    setModalTitle('编辑大学');
    setCurrentRecord(record);
    form.setFieldsValue(record);
    setModalVisible(true);
  };

  const handleDelete = (record: any) => {
    Modal.confirm({
      title: '确认删除',
      content: `确定要删除大学"${record.universityName}"吗？`,
      onOk: async () => {
        try {
          await deleteUniversity(record.universityId);
          message.success('删除成功');
          fetchData();
        } catch (error) {
          message.error('删除失败');
        }
      },
    });
  };

  const handleModalOk = async () => {
    try {
      const values = await form.validateFields();
      if (currentRecord) {
        await updateUniversity({ ...values, universityId: currentRecord.universityId });
        message.success('更新成功');
      } else {
        await addUniversity(values);
        message.success('添加成功');
      }
      setModalVisible(false);
      fetchData();
    } catch (error) {
      message.error('操作失败');
    }
  };

  return (
    <PageContainer>
      <Card>
        <div style={{ marginBottom: 16 }}>
          <Button type="primary" icon={<PlusOutlined />} onClick={handleAdd}>
            新增大学
          </Button>
        </div>
        <Table
          columns={columns}
          dataSource={data}
          rowKey="universityId"
          loading={loading}
        />
      </Card>

      <Modal
        title={modalTitle}
        visible={modalVisible}
        onOk={handleModalOk}
        onCancel={() => setModalVisible(false)}
        destroyOnHidden
      >
        <Form
          form={form}
          layout="vertical"
        >
          <Form.Item
            name="universityName"
            label="大学名称"
            rules={[{ required: true, message: '请输入大学名称' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="universityCode"
            label="大学代码"
            rules={[{ required: true, message: '请输入大学代码' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="universityType"
            label="大学类型"
            rules={[{ required: true, message: '请选择大学类型' }]}
          >
            <Select>
              <Option value="综合">综合</Option>
              <Option value="理工">理工</Option>
              <Option value="师范">师范</Option>
              <Option value="农林">农林</Option>
              <Option value="医药">医药</Option>
              <Option value="其他">其他</Option>
            </Select>
          </Form.Item>
          <Form.Item
            name="province"
            label="所属省份"
            rules={[{ required: true, message: '请输入所属省份' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="city"
            label="所属城市"
            rules={[{ required: true, message: '请输入所属城市' }]}
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="address"
            label="大学地址"
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="introduction"
            label="大学简介"
          >
            <Input.TextArea rows={4} />
          </Form.Item>
          <Form.Item
            name="logoUrl"
            label="Logo URL"
          >
            <Input />
          </Form.Item>
          <Form.Item
            name="status"
            label="状态"
            initialValue="0"
          >
            <Select>
              <Option value="0">正常</Option>
              <Option value="1">停用</Option>
            </Select>
          </Form.Item>
        </Form>
      </Modal>
    </PageContainer>
  );
};

export default University; 